﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Boss : MonoBehaviour
{
    public int BossHp, Speed;
    public Text HurtTipsText;
    public float t1, t2;
    public bool IsShow;
    public GameObject gamepanel;
    void Start()
    {
        HurtTipsText.gameObject.SetActive(false);
        Speed = 5;
        BossHp = GamePanel.instance.BossHp;
        gamepanel = GameObject.FindGameObjectWithTag("GamePanel");
        GameObject obj2 = Instantiate(Resources.Load<GameObject>("Boom"));
        Destroy(obj2, 2);
    }

    /// <summary>
    /// /boss掉血
    /// </summary>
    /// <param name="hurt"></param>
    public void LoseHp(int hurt)
    {
        BossHp -= hurt;
        Debug.Log("boss还剩余" + BossHp + "血");
        if (BossHp <= 0)
        {
            GamePanel.instance.IsJieSuan = true;
            GamePanel.instance.BossDeath = true;
            Destroy(gameObject);
            DataManager.Instance.XiaoMieBossNum += 1;
            DataManager.Instance.Save();
        }
    }
    public void ShowTips(string str)
    {
        HurtTipsText.gameObject.SetActive(true);
        HurtTipsText.text = str;
        IsShow = true;
    }

    // Update is called once per frame
    void Update()
    {
        transform.Translate(Speed, 0, 0);
        if (gameObject.transform.localPosition.x > 306)
        {
            Speed *= -1;
        }
        if (gameObject.transform.localPosition.x < -306)
        {
            Speed *= -1;
        }
        if (!GamePanel.instance.IsDeath)
        {
            t2 += Time.deltaTime;
            if (t2 > 3f)
            {
                GameObject o = GameTools.instance.GetGameobject(Balloon.Boom);
                o.transform.SetParent(gamepanel.transform);
                o.transform.localPosition = transform.localPosition;
                o.GetComponent<Rigidbody2D>().gravityScale = 50;
                t2 = 0;
            }
        }



        if (IsShow)
        {
            t1 += Time.deltaTime;
            if (t1 < 0.2f)
            {

                HurtTipsText.transform.Translate(0, 10, 0);
            }
            else
            {
                HurtTipsText.gameObject.SetActive(false);
                HurtTipsText.transform.localPosition = Vector3.zero;
                t1 = 0;

                IsShow = false;
            }
        }


    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag.Equals("Bullet"))
        {
            LoseHp(MainPanel.instance.Hurt);
            ShowTips(MainPanel.instance.Hurt.ToString());
            GamePanel.instance.JinDuBoss();
           

        }
    }

}
